library(tidyverse)
# Create a grid of points
x <- seq(-10, 10, by = 1)
y <- seq(-10, 10, by = 1)
# Prepare Data
grid <-
expand_grid(x = x, y = y) |>
mutate(u = -y, v = x) |>
mutate(norm = sqrt(x^2 + y^2)) |>
mutate(u = u/max(norm), v = v/max(norm))
# Plot the vector field
ggplot(grid, aes(x = x, y = y)) +
geom_segment(
aes(xend = x + u, yend = y + v),
arrow = arrow(length = unit(0.1, "cm"))
) +
theme_minimal() +
labs(title = "Vector Field of f(x,y) = (-y, x)",
x = "X", y = "Y"
)